//
//  NSString+AFFApp.m
//  AnyfishApp
//
//  Created by apple on 14-3-25.
//  Copyright (c) 2014年 Anyfish. All rights reserved.
//

#import "NSString+AFF.h"

@implementation NSString (AFFApp)

static const NSArray *awesomeStrings;

+ (NSString *)stringFromAwesomeIcon:(FAIcon)icon
{
    if(!awesomeStrings) {
        awesomeStrings = [NSArray arrayWithObjects:@"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", nil];
    }
    
    return [awesomeStrings objectAtIndex:icon];
}

+ (NSString*)appendString:(NSArray<NSString*>*)strings {
    NSString *string = [[NSString alloc] init];
    
    for (int i = 0; i < strings.count; i++) {
        string = [string stringByAppendingFormat:@"%@%@", strings[i], (i == strings.count - 1) ? @"" : @"┇┇" ];
    }
    
    /*for(NSString *str in strings){//相同字符串会引起判断出错
        string = [string stringByAppendingFormat:@"%@%@", str, [str isEqual:[strings lastObject]]?@"":kSperate_Char];
    }*/
    
    return string;
}



- (NSString *)trimWhitespace
{
    NSMutableString *str = [self mutableCopy];
    CFStringTrimWhitespace((__bridge CFMutableStringRef)str);
    return str;
}

+ (BOOL)isNilOrEmpty:(NSString *)str{
    if(str){
        return [str isEmpty];
    }
    
    return YES;
}

+ (BOOL)isEmptyString:(NSString *)str {
    if ([str isEqualToString:@""]) {
        return NO;
    }
    if(str==nil)
        return YES;
    
    return [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0;
}

- (BOOL)isLegalName:(NSString*)name {
    if([NSString isNilOrEmpty:name])
        return YES;
    
    NSArray *filted = @[@"百鱼",
                        @"Anyfish",
                        @"anyfish",
                        @"ANYFISH",
                        ];
    
    for(NSString *str in filted){
        if([name rangeOfString:str options:NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch].length>0){
            return NO;
        }
    }
    
    return YES;
}

- (BOOL)isEmpty
{
    return self.length==0;
//    return [[self trimWhitespace] isEqualToString:@""];
}

- (BOOL)isIncludeChineseInString{
    for (int i=0; i<self.length; i++) {
        unichar ch = [self characterAtIndex:i];
        if (0x4e00 < ch  && ch < 0x9fff) {
            return YES;
        }
    }
    return NO;
}

- (NSUInteger)numberOfLines
{
    NSUInteger lines = [self componentsSeparatedByString:@"\n"].count + 1;
    //DDLogInfo(@"%d",lines);
    return lines;
}

//NSString是否是纯数字
- (BOOL)isPureInt {
    NSScanner* scan = [NSScanner scannerWithString:self];
    int val;
    return[scan scanInt:&val] && [scan isAtEnd];
}


//判断字符串是否包含空格
- (BOOL)hasWhitespace {
    BOOL hasSpace = NO;
    NSMutableString *newStr = [NSMutableString stringWithString:self];
    NSRange range = [newStr rangeOfString:@" "];
    if (range.length) {
        hasSpace = YES;
    }
    return hasSpace;
}

- (NSString*)stringWithAES_HEXBytes{
    NSString *returnStr = @"";
    returnStr = [self aes256_encrypt:AESKey Encrypttext:self];
    return returnStr;
}

- (NSString*)stringDecryptAES{
    NSString *returnStr = @"";
    returnStr = [self aes256_decrypt:AESKey Decrypttext:self];
    return returnStr;
}

@end
